﻿using MB.RuleBase.Common;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business
{
    public class ProduceGoods
    {
        /// <summary>
        /// 获取所有标签控制器IP列表和标签ID范围
        /// </summary>
        /// <returns></returns>
        public List<PFT_IPListWithIDrange> getIPaddressListWithIDrange()
        {
            try
            {
                List<PFT_IPListWithIDrange> list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<PFT_IPListWithIDrange>("ProduceGoods", "getIPaddressListWithIDrange");
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        /// <summary>
        /// 拍电子标签写入数据库
        /// </summary>
        /// <param name="IPaddress">电子标签地址</param>
        /// <param name="LabelID">电子标签ID</param>
        /// <param name="QTYexce">实拍数量</param>
        /// <returns></returns>
        public bool PressElecLabelReaction(string IPaddress, string LabelID, int QTYexce, out string errorMsg)
        {
            errorMsg = "";
            bool _result = false;
            var oracle = new MB.RuleBase.BulkCopy.SimulatedOracleHelper();
            var db = MB.Orm.Persistence.DatabaseHelper.CreateDatabase();
            var cn = oracle.CreateOracleConnection(db.ConnectionString);
            System.Data.Common.DbTransaction tran = null;
            try
            {
                cn.Open();
                tran = cn.BeginTransaction();
                _result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery(tran, "ProduceGoods", "ElecLabelPressReaction", QTYexce, IPaddress, LabelID) > 0;
                if (_result)
                {
                    tran.Commit();
                    return true;
                }
                else
                {
                    errorMsg = "回写实拍数量失败，数据回滚";
                    tran.Rollback(); return false;
                }
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                tran.Rollback();
                return false;
            }
            finally
            {
                if (tran != null) tran.Dispose();
                if (cn != null) cn.Close();
                if (cn != null) cn.Dispose();
            }
        }

        /// <summary>
        /// 修改明细信息，状态为亮灯
        /// </summary>
        /// <param name="ProduceBatch">扫描批次</param>
        /// <param name="sTagId">扫描枪ID</param>
        /// <returns></returns>
        public bool updateLightElecLable(string ProduceBatch, string sTagId, out string msg)
        {
            try
            {
                bool result = DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("ProduceGoods", "SetproducesaleorderbomitemFCID", sTagId) > 0;
                msg = "亮灯状态修改正常";
                return DatabaseExcuteByXmlHelper.NewInstance.ExecuteNonQuery("ProduceGoods", "updateProduceLightLabelstatus", ProduceBatch, sTagId) > 0;
            }
            catch (Exception ex)
            {
                msg = "异常:" + ex.Message;
                MB.Util.TraceEx.Write(ex.Message);
                return false;
            }
        }

        /// <summary>
        /// 获取扫码枪值进行亮灯
        /// </summary>
        /// <param name="ProduceBatch">扫描批次</param>
        /// <param name="HandScanerID">扫描枪ID</param>
        /// <returns></returns>
        public List<ProduceLightLabel> getLightElecLabel(string ProduceBatch, string HandScanerID)
        {
            try
            {
                return DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<ProduceLightLabel>("ProduceGoods", "getProduceLightLabelQTY", ProduceBatch, HandScanerID);
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;//占用状态
            }
        }

        public List<MachingDetailModel> getAllMachingDetail()
        {
            try
            {
                List<MachingDetailModel> list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachingDetailModel>("ProduceGoods", "getAllMacingdetail");
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 扫描商品条码打印数据获取
        /// </summary>
        /// <param name="GoodsCode">商品编码</param>
        /// <returns></returns>
        public MachingDetailModel getAllMachingDetailByGoodsCode(string GoodsCode)
        {
            try
            {
                MachingDetailModel list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachingDetailModel>("ProduceGoods", "getAllMacingdetailByGoodsCode", GoodsCode).FirstOrDefault();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 批次打印条码信息
        /// </summary>
        /// <param name="BatchNumber"></param>
        /// <returns></returns>
        public List<MachingDetailModel> getAllMachingDetailByBatchNumber(string BatchNumber)
        {
            try
            {
                List<MachingDetailModel> list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachingDetailModel>("ProduceGoods", "getAllMacingdetailByBatchNumber", BatchNumber);
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        /// 批次数据打印获取
        /// </summary>
        /// <param name="batchNo">批次号</param>
        /// <returns></returns>
        public List<MachingDetailModel> getProduceBatchNO(string batchNo)
        {
            try
            {
                List<MachingDetailModel> _goods = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachingDetailModel>("ProduceGoods", "getProduceBatchNO", batchNo);

                if (_goods != null)
                    return _goods;
                else
                    return null;
            }
            catch (Exception ex)
            {
                MB.Util.TraceEx.Write(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 批次分拣条码数据打印
        /// </summary>
        /// <param name="BatchNO">批次号</param>
        /// <returns></returns>
        public MachingDetailModel getAllMachingDetailByProduceBatchNO(string BatchNO)
        {
            try
            {
                MachingDetailModel list = DatabaseExcuteByXmlHelper.NewInstance.GetObjectsByXml<MachingDetailModel>("ProduceGoods", "getAllMacingdetailByBatchNO", BatchNO).FirstOrDefault();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
